package edu.uoc.tdp.pac4.database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import edu.uoc.tdp.pac4.beans.Aula;

public class RoomManager {
	Connection con = null;
	PreparedStatement pst = null;
	ResultSet rs = null;
	DateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
	
	public RoomManager () {
		
	}
	
	private Connection connBBDD () throws SQLException {
		try {
			con = DiskManager.getInstance().getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return con;
	}


	public Aula findRoom(String paramCerca) {
		// TODO Auto-generated method stub
		return null;
	}

	public void createRoom(Aula aula) {
		// TODO Auto-generated method stub
		
	}

	public void updateRoom(Aula aula) {
		// TODO Auto-generated method stub
		
	}
	
	public List<Aula> findAllRooms() {

		List<Aula> aules = new ArrayList<Aula>();
		Aula aula = null;
		try {
			String querystring = "SELECT id, nom FROM aula";
			con = connBBDD();

			pst = con.prepareStatement(querystring);
			rs = pst.executeQuery();
			while (rs.next()) {

				aula = new Aula();
				aula.setIdAula(rs.getInt(1));
				aula.setNom(rs.getString(2));
				aules.add(aula);

				//
				//
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (pst != null)
					pst.close();
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return aules;
	}

	public Aula findRoomByName(String nom) {

		Aula aula = null;

		try {
			String querystring = "SELECT id, nom FROM aula WHERE nom=? ";
			con = connBBDD();
			pst = con.prepareStatement(querystring);
			pst.setString(1, nom);

			rs = pst.executeQuery();
			if (rs.next()) {
				aula = new Aula();
				aula.setIdAula(rs.getInt(1));
				aula.setNom(rs.getString(2));

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (pst != null)
					pst.close();
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return aula;
	}

	public Aula findRoomByPrimaryKey(int aulaid) {

		Aula aula = null;

		try {
			String querystring = "SELECT id, nom, capacitat FROM aula WHERE id=? ";
			con = connBBDD();
			pst = con.prepareStatement(querystring);
			pst.setInt(1, aulaid);

			rs = pst.executeQuery();
			if (rs.next()) {
				aula = new Aula();

				aula.setIdAula(rs.getInt(1));
				aula.setNom(rs.getString(2));
				aula.setCapacitat(rs.getInt(3));

				//
				// id integer NOT NULL,
				// nom character varying(30) NOT NULL,
				// capacitat integer NOT NULL,
				// tipusaula tipusaula NOT NULL,
				// localitzacio character varying(50) NOT NULL,
				// datainactivitat date,

			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (pst != null)
					pst.close();
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		return aula;
	}


}
